<?php
namespace core;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of FastPHP
 *
 * @author gangguo
 */
class FastPHP {
    //put your code here
    public static $classMap = array();
    public $assign = array();


    public function __construct() {
        date_default_timezone_set('Asia/Shanghai');//初始化时区
    }
    
    
    static public function run(){
        date_default_timezone_set('Asia/Shanghai');//初始化时区
        \core\lib\log::init();

        $route = new \core\lib\route();
        $ctlClass = $route->ctl;
        $actFun = $route->act;
        $ctlFile = FastPHP.'/'.APP.'/ctl/'.$ctlClass.'Ctl.php';
        $ctlNew = '\\'.APP.'\ctl\\'.$ctlClass.'Ctl';
        if(is_file($ctlFile)){
            include $ctlFile;
            $Ctl = new $ctlNew();
            $Ctl->$actFun();
            \core\lib\log::log('controller:'.$ctlClass.'; action:'.$actFun.';');
        }else{
            throw new \Exception('找不到控制器:'.$ctlFile);
        }
        
    }
    
    static public function load($class){
        //自动加载类库
        if(isset($classMap[$class])){
            return TRUE;
        }else{
            $class = str_replace('\\', '/', $class);
            $file = FastPHP.'/'.$class.'.php';
            if(is_file($file)){
                include $file;
                self::$classMap[$class] = $class;
            }else{
                return FALSE;
            }
        }
    }
    
    
    public function assign($name,$value){
        $this->assign[$name] = $value;
    }
    
    public function display($file){
        $file_path = APP.'/view/'.$file.'.html';
        if(is_file($file_path)){
            //extract($this->assign);
            \Twig_Autoloader::register();
            
            $loader = new \Twig_Loader_Filesystem(APP.'/view');
            $twig = new \Twig_Environment($loader, array(
                'cache' => FastPHP.'/runtime/cache/',
                'debug' => DEBUG
            ));
            $template = $twig->loadTemplate($file.'.html');
            $template->display($this->assign?$this->assign:'');
            
            
        }
        
    }
}
